//
//  SubDayView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/6.
//

import UIKit

class SubDayView: UIView {

    lazy var dayColView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumInteritemSpacing = 6
        layout.minimumLineSpacing = 6
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: (ScreenW-90)*0.16, height: (ScreenW-90)*0.20)
        let col = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        col.showsVerticalScrollIndicator = false
        col.showsHorizontalScrollIndicator = false
        col.backgroundColor = .clear
        col.delegate = self
        col.dataSource = self
        col.register(SubDayColCell.self, forCellWithReuseIdentifier: SubDayColCell.identifier)
        return col
    }()

    var dayArr = [TicketModel]() {
        didSet {
            dayColView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(named: "white_FBF6F1")
        addSubview(dayColView)
        dayColView.snp.makeConstraints { make in
            make.top.left.equalTo(10)
            make.bottom.right.equalTo(-10)
        }
        wn_cornerWithBorder(8)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension SubDayView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dayArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SubDayColCell.identifier, for: indexPath) as! SubDayColCell
        cell.model = dayArr[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SubDayColCell.identifier, for: indexPath) as! SubDayColCell
        let popMenu = SwiftPopMenu(menuWidth: 100, arrow: .zero, datas: ["Golds * \(dayArr[indexPath.item].gold ?? 0)"], configures: [SwiftPopMenuConfigure.popMenuItemHeight(30)])
        popMenu.didSelectMenuBlock = { index in
            popMenu.dismiss()
        }
        popMenu.arrowPoint = .init(x: cell.left+(ScreenW-90)*0.16+6, y: cell.bottom+(ScreenW-90)*0.20*2+6)
        popMenu.show()

    }
}
